/*
 * TaskStateMachineValueDisplay.java
 *
 * Created on 02 February 2004, 09:17
 * Copyright (C) 2004, Velocityme Partnership
 */

package com.velocityme.client.gui.node.taskstatemachine;

import com.velocityme.client.gui.ListAssignPanel;
import com.velocityme.valueobjects.NodeValue;
import com.velocityme.valueobjects.RoleValue;
import com.velocityme.valueobjects.TaskStateMachineValue;
import com.velocityme.valueobjects.TransitionValue;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

/**
 *
 * @author  Robert Crida Work
 */
public class TaskStateMachineValueDisplay extends javax.swing.JPanel {
    
    private TaskStateMachineValue m_taskStateMachineValue;
    private TransitionTableModel m_modelTransition;
    private ListAssignPanel m_listInterestedRolesPanel;
    private Collection m_originalInterestedRoleValues;
    
    /** Creates new form TaskStateMachineValueDisplay */
    public TaskStateMachineValueDisplay(TaskStateMachineValue p_taskStateMachineValue, TransitionValue[] p_transitionValues, RoleValue[] p_allRoleValues) {
        initComponents();
        java.awt.GridBagConstraints gridBagConstraints;
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.gridx = 2;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 0.25;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 0);
        Collection allRoleValues = new ArrayList();
        if (p_allRoleValues != null)
            for(int i = 0; i < p_allRoleValues.length; i++)
                allRoleValues.add(p_allRoleValues[i]);
        add(m_listInterestedRolesPanel = new ListAssignPanel(allRoleValues), gridBagConstraints);
        setTaskStateMachineValue(p_taskStateMachineValue, p_transitionValues, p_allRoleValues);
    }
    
    public void setTaskStateMachineValue(TaskStateMachineValue p_taskStateMachineValue, TransitionValue[] p_transitionValues, RoleValue[] p_allRoleValues) {
        NodeValue nodeValue = p_taskStateMachineValue.getNodeValue();
        if (nodeValue != null) {
            if (nodeValue.nameHasBeenSet())
                jTextFieldName.setText(nodeValue.getName());
            if (nodeValue.descriptionHasBeenSet())
                jTextAreaDescription.setText(nodeValue.getDescription());
        }
        m_modelTransition.setTransitionValues(p_transitionValues);
        m_originalInterestedRoleValues = new ArrayList();
        for(int i = 0; i < p_taskStateMachineValue.getInterestedRoleValues().length; i++)
            m_originalInterestedRoleValues.add(p_taskStateMachineValue.getInterestedRoleValues()[i]);
        m_listInterestedRolesPanel.setAssignedValues(m_originalInterestedRoleValues);
        m_taskStateMachineValue = p_taskStateMachineValue;
    }
    
    public TaskStateMachineValue getTaskStateMachineValue() {
        if (m_taskStateMachineValue.getNodeValue() == null)
            m_taskStateMachineValue.setNodeValue(new NodeValue());
        Collection newRoleValues = new ArrayList(m_listInterestedRolesPanel.getAssignedValues());
        RoleValue[] roleValues = new RoleValue[newRoleValues.size()];
        Iterator i = newRoleValues.iterator();
        int index = 0;
        while(i.hasNext()) {
            roleValues[index++] = (RoleValue)i.next();
        }
        m_taskStateMachineValue.setInterestedRoleValues(roleValues);
        m_taskStateMachineValue.getNodeValue().setName(jTextFieldName.getText());
        m_taskStateMachineValue.getNodeValue().setDescription(jTextAreaDescription.getText());
        return m_taskStateMachineValue;
    }
    
    public void setEditable(boolean p_editable) {
        jTextFieldName.setEditable(p_editable);
        jTextAreaDescription.setEditable(p_editable);
        jLabelTransitions.setVisible(!p_editable);
        jScrollPaneTransitions.setVisible(!p_editable);
        m_listInterestedRolesPanel.setEnabled(p_editable);
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    private void initComponents() {//GEN-BEGIN:initComponents
        java.awt.GridBagConstraints gridBagConstraints;

        jLabel1 = new javax.swing.JLabel();
        jLabelName = new javax.swing.JLabel();
        jTextFieldName = new javax.swing.JTextField();
        jLabelDescription = new javax.swing.JLabel();
        jScrollPaneDescription = new javax.swing.JScrollPane();
        jTextAreaDescription = new javax.swing.JTextArea();
        jLabelTransitions = new javax.swing.JLabel();
        jScrollPaneTransitions = new javax.swing.JScrollPane();
        m_modelTransition = new TransitionTableModel();
        jTableTransition = new javax.swing.JTable(m_modelTransition);
        jLabelInterested = new javax.swing.JLabel();

        setLayout(new java.awt.GridBagLayout());

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/velocityme/client/icons/task-32x32.png")));
        add(jLabel1, new java.awt.GridBagConstraints());

        jLabelName.setText("Name:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        add(jLabelName, gridBagConstraints);

        jTextFieldName.setEditable(false);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        add(jTextFieldName, gridBagConstraints);

        jLabelDescription.setText("Description:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        add(jLabelDescription, gridBagConstraints);

        jTextAreaDescription.setEditable(false);
        jTextAreaDescription.setLineWrap(true);
        jTextAreaDescription.setWrapStyleWord(true);
        jScrollPaneDescription.setViewportView(jTextAreaDescription);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weighty = 0.5;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        add(jScrollPaneDescription, gridBagConstraints);

        jLabelTransitions.setText("Transitions:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        add(jLabelTransitions, gridBagConstraints);

        jTableTransition.setPreferredScrollableViewportSize(new java.awt.Dimension(0, 200));
        jScrollPaneTransitions.setViewportView(jTableTransition);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weighty = 0.5;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        add(jScrollPaneTransitions, gridBagConstraints);

        jLabelInterested.setText("Interested Roles:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        add(jLabelInterested, gridBagConstraints);

    }//GEN-END:initComponents
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabelDescription;
    private javax.swing.JLabel jLabelInterested;
    private javax.swing.JLabel jLabelName;
    private javax.swing.JLabel jLabelTransitions;
    private javax.swing.JScrollPane jScrollPaneDescription;
    private javax.swing.JScrollPane jScrollPaneTransitions;
    private javax.swing.JTable jTableTransition;
    private javax.swing.JTextArea jTextAreaDescription;
    private javax.swing.JTextField jTextFieldName;
    // End of variables declaration//GEN-END:variables
    
}
